import { http } from "@/utils/http";

/**
 数据字典接口
 **/
export const queryDictData = (data?: any) => {
  if (data?.typeCode === "ERC_FACTORY_AREA") {
    // 厂区隔离，HSE地块数据改为，根据当前厂区及本地储存的hseFacAreaList获取
    const facCode = localStorage.getItem("facCode");
    const hseFacAreaList = localStorage.getItem("hseFacAreaList");
    if (facCode && hseFacAreaList) {
      const arr = JSON.parse(hseFacAreaList).filter(item => item.parentCode === facCode);
      const newHseFacAreaList = arr.map(item => ({ ...item, value: item.code, id: item.code }));
      return newHseFacAreaList;
    }
    return [];
  } else {
    return http.request<Array<any>>("post", "/account/api/v1/publish/dictItem/queryListByTypeCode", {
      data
    });
  }
};

// 根据typeCode和itemValue查询子项
export const queryChildByTypeCodeAndItemValue = (data?: object) => {
  return http.request<ResultTable>("post", `/account/api/v1/publish/dictItem/queryChildByTypeCodeAndItemValue`, {
    data
  });
};

// 查询系统下拉树接口
export const querySystemTree = (data?: object) => {
  return http.request("post", "/digitaltwins/api/v1/systemCategory/queryListTree", {
    data
  });
};
// 查询设备总称下拉列表接口(新)
export const queryEquipCategorySelect = (data?: object) => {
  return http.request("post", "/digitaltwins/api/v1/equipmentCategory/queryOption", {
    data
  });
};
// 查询设备类型下拉列表接口(新)
export const queryEquipTypeSelect = (data?: object) => {
  return http.request("post", "/digitaltwins/api/v1/equipmentType/queryOption", {
    data
  });
};
// 查询设备型号下拉列表接口(新)
export const queryEquipModelSelect = (data?: object) => {
  return http.request("post", "/digitaltwins/api/v1/equipmentModel/queryOption", {
    data
  });
};
// 查询设备编号下拉列表接口
export const queryEquipIdSelect = (data?: object) => {
  return http.request("post", "/digitaltwins/api/v1/publish/equipment/queryEqpList", {
    data
  });
};

/**
 文件接口
 **/
// 删除文件接口
export const deleteFile = (data?: object) => {
  return http.request("post", "/sharing/api/v1/file/delete", {
    data
  });
};

// dms上传文件删除接口
export const deleteFileDms = (data?: Record<string, any>) => {
  return http.request("post", "/dms/api/v1/doc/del", {
    data
  });
};

// 获取当前登录人科室列表
export const queryCurrentClassList = async () => {
  const res = await http.request("post", "/account/api/v1/class/query", {});
  return res?.map(item => ({
    ...item,
    name: item.itemName,
    value: item.itemValue
  }));
};

// 获取当前登录人厂区和科室列表
export const queryFacAndClassList = async () => {
  const res = await http.request("post", "/account/api/v1/permission/data/query", {});
  return res;
};

// 获取菜单树
export const queryMenuTree = async () => {
  const res = await http.request("post", "/account/api/v1/menuCollect/treeMenuSearch");
  return res;
};

export const querySessionApi = async (data?: Record<string, any>) => {
  return http.request<any[]>("post", "/account/api/v1/agent/query", {
    data
  });
};

export const addSessionApi = async (data?: Record<string, any>) => {
  return http.request("post", "/account/api/v1/agent/create", {
    data
  });
};
